/**
 * 图片展示管理初始化
 */
var Picture = {

};

/**
 * 页面初始化
 */
$(function () {
    //初始化图片选择
    selectImgTake.init('selectItemDiv',5);

    //初始化分页插件
    loadpage();

    //鼠标悬停放大图片
    showImg();
});

/**
 * 鼠标悬停放大图片
 */
function showImg(){
    var x = 10;
    var y = 20;
    $("img").mouseover(function(e){
        //创建 div 元素
        var tooltip = "<div id='tooltip'><img src='"+ this.src +"' alt='预览图'/><\/div>";
        $("body").append(tooltip);	//把它追加到文档中
        $("#tooltip")
            .css({
                "top": (e.pageY+y) + "px",
                "left":  (e.pageX+x)  + "px"//设置x坐标和y坐标，并且显示
            }).show("fast");
    }).mouseout(function(){
        $("#tooltip").remove();
    }).mousemove(function(e){
        $("#tooltip")
            .css({
                "top": (e.pageY+y) + "px",
                "left":  (e.pageX+x)  + "px"
            });
    });
}

/**
 * 打开上传图片
 */
Picture.openAddPicture = function () {
    var index = layer.open({
        type: 2,
        title: '上传图片',
        area: ['1000px', '650px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/picture/picture_add'
    });
    this.layerIndex = index;
};

/**
 * 判断是否选择了图片
 */
Picture.check = function(){
    //获得选中图片
    var selectImgDivs = selectImgTake.getSelectImgs("selectImgDiv");
    if(selectImgDivs.length < 1){
        Feng.info("请选择一张图片!");
        return false;
    }else{
        Picture.selectImgDivs = selectImgDivs;
        return true;
    }
}


/**
 * 删除图片
 */
Picture.delete = function () {
    if(!Picture.check()){
        return;
    }
    var operation = function () {
        for(var i=0; i < Picture.selectImgDivs.length;i++){
            var ajax = new $ax(Feng.ctxPath + "/picture/delete", function (data) {
                Feng.success("删除成功!");
                loadData();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            var pictureId = Picture.selectImgDivs[i].id;
            pictureId = pictureId.substring(4);
            ajax.set("pictureId", pictureId);
            ajax.start();
        }
    }
    Feng.confirm("是否彻底删除?", operation);
};

/**
 * 查询
 */
Picture.search = function () {
    loadData(Feng.getUrlParam("currentPage"),$("#condition").val(),$("#beginTime").val(),$("#endTime").val());
};

/**
 * 重置查询
 */
Picture.resetSearch = function () {
    loadData();
};


/**
 * 重新初始化分页插件
 */
function loadpage() {
    //总记录数
    var myTotal = parseInt($("#total").val());
    //每页的记录数
    var myPageSize = parseInt($("#PageSize").val());
    //计算出的页数
    var countindex = myTotal % myPageSize > 0 ? (myTotal / myPageSize) + 1 : (myTotal / myPageSize);
    $("#countindex").val(countindex);
    //当前的页面数
    var currentPage = Feng.getUrlParam("currentPage");
    if(!currentPage){
        currentPage = 1;
    }

    $.jqPaginator('#pagination', {
        totalPages: parseInt($("#countindex").val()),
        visiblePages: parseInt($("#visiblePages").val()),
        currentPage: parseInt(currentPage),
        first: '<li class="first"><a href="javascript:;">首页</a></li>',
        prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>上一页</a></li>',
        next: '<li class="next"><a href="javascript:;">下一页<i class="arrow arrow3"></i></a></li>',
        last: '<li class="last"><a href="javascript:;">末页</a></li>',
        page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
        onPageChange: function (num, type) {
            if (type == "change") {
                //点击分页查询
                loadData(num);
            }
        }
    });
}

/**
 * 点击分页查询
 * @param num 页码
 */
function loadData(num,condition,beginTime,endTime) {
    if(!num){
        num = 1;
    }
    if(!condition){
        condition = "";
    }
    if(!beginTime){
        beginTime = "";
    }
    if(!endTime){
        endTime = "";
    }
    var limit = parseInt($("#PageSize").val());
    var offset = (num - 1) * parseInt($("#PageSize").val());
    //跳转到其它页
    window.location.href = Feng.ctxPath + "/picture?limit=" + limit
        + "&offset=" + offset
        + "&currentPage=" + num
        + "&condition=" + condition
        + "&beginTime=" + beginTime
        + "&endTime=" + endTime;
}



